package com.zxw.article.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.article.service.ArticleCategoryService;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.constant.Constants;
import com.zxw.common.entity.category.ArticleCategoryEntity;
import com.zxw.common.entity.category.ArticleCategoryListQuery;
import com.zxw.common.entity.category.ArticleCategoryQuery;
import com.zxw.common.entity.category.ArticleCategoryVO;
import com.zxw.common.enums.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 文章分类表接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-19
 */
@Api(tags = "文章分类表接口", description = "ArticleCategoryController")
@RestController
@RequestMapping(ApiConst.API_PREFIX + "/article/category")
public class ArticleCategoryController {

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody ArticleCategoryQuery articleCategoryQuery) {
        articleCategoryService.save(articleCategoryQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<ArticleCategoryVO>> pageList(ArticleCategoryListQuery articleCategoryListQuery) {
        IPage<ArticleCategoryVO> iPage = this.articleCategoryService.pageList(articleCategoryListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("列表条件查询")
    @GetMapping("/queryList")
    public ApiResult<List<ArticleCategoryVO>> queryList(ArticleCategoryListQuery articleCategoryListQuery) {
        List<ArticleCategoryVO> list = this.articleCategoryService.queryList(articleCategoryListQuery);
        return ApiResult.success(list);
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<ArticleCategoryQuery> list) {
        articleCategoryService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody ArticleCategoryQuery articleCategoryQuery) {
        articleCategoryService.updateById(articleCategoryQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        articleCategoryService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public ApiResult<ArticleCategoryVO> detail(@PathVariable("id") String id) {
        ArticleCategoryEntity articleCategoryEntity = articleCategoryService.getById(id);
        ArticleCategoryVO articleCategoryVO = new ArticleCategoryVO();
        BeanUtils.copyProperties(articleCategoryEntity, articleCategoryVO);
        return ApiResult.success(articleCategoryVO);
    }


}

